const { logfn } = require("./logfn.js");
const { JSDOM } = require("jsdom");
const axios = require('axios');
const upvoteFirstPost = async () => {
  try {
    const { data } = await axios.get("https://www.hahamx.cn/joke/2942537");
    //console.log(data)
    const dom = new JSDOM(data, {
      runScripts: "dangerously",
      resources: "usable"
    });
    //console.log(dom)
    const { document } = dom.window;
    const firstPost = document.querySelector("a.btn-icon-good");
    console.log(firstPost.textContent)
    firstPost.click();
    const isUpvoted = firstPost.classList.contains("btn-icon-good-highlight");
    const msg = isUpvoted
      ? "Post has been upvoted successfully!"
      : "The post has not been upvoted!";
    return msg;
  } catch (error) {
    throw error;
  }
};
upvoteFirstPost().then(msg => msg && console.log(msg));